package io.renren.service.impl;

import io.renren.dao.*;
import io.renren.entity.*;
import io.renren.service.BaseService;
import io.renren.service.CommonSyncToB1Service;
import io.renren.utils.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by wdj on 2017/8/2.
 */
@Service("commonSyncToB1Service")
public class CommonSyncToB1ServiceImpl extends BaseService implements CommonSyncToB1Service {

    @Autowired
    private CustomSettlementDao customSettlementDao;

    @Autowired
    private ServiceSettlementDetailDao serviceSettlementDetailDao;

    @Autowired
    private SocialSecurityPayDetailDao socialSecurityPayDetailDao;

    /**
     * 客户结算 同步B1接口
     * @throws Exception
     */
    @Override
    @Transactional
    public void cusSettleSyncToB1() throws Exception {
        //拼接参数
        Map<String, Object> queryParams = new HashedMap();
        List<String> data = new ArrayList(); //请求中data参数集合
        //1.查出所有的结算点
         List<CustomSettlementEntity> customSettlementEntities = customSettlementDao.getSettlementList();
        for (CustomSettlementEntity customSettlementEntity:customSettlementEntities) {

            Map<String, Object> paramsBySettleId = new HashedMap();

            paramsBySettleId.put("deaID",customSettlementEntity.getContractNo());               //合同编号
            paramsBySettleId.put("User",customSettlementEntity.getDutyPerson());                //责任人
            paramsBySettleId.put("Status","1");              //收款状态
            String DocNum = "A" + DateUtils.format(new Date(),"yyyyMMddHHmmss");
            paramsBySettleId.put("DocNum",DocNum);              //单据编号
            paramsBySettleId.put("CardName",customSettlementEntity.getUnitName());            //客户名称
            paramsBySettleId.put("Dept",customSettlementEntity.getDutyDepartment());                //责任部门

            List<String> settlementDetails = new ArrayList(); //明细集合
            Map<String,Object> params = new HashMap<>();
            //查询出该结算点下待同步的劳务结算汇总表数据集合，以后可以带分年分月
            params.put("settlementid",customSettlementEntity.getId());
            List<ServiceSettlementDetailEntity> serviceSettlementDetailEntities = serviceSettlementDetailDao.getServiceSettlemetnByPatch(params);
            for (ServiceSettlementDetailEntity serviceSettlementDetailEntity:serviceSettlementDetailEntities) {
                Map<String, Object> tmp_data = new HashedMap();
                tmp_data.put("umanage",serviceSettlementDetailEntity.getJsdwGuanlifSs());                 //单位管理费
                tmp_data.put("oTax",serviceSettlementDetailEntity.getJsqtTaxSs());                    //其它税金
                tmp_data.put("uaccIDent",serviceSettlementDetailEntity.getJsdwYiwaiSs());               //单位意外
                tmp_data.put("pfund",serviceSettlementDetailEntity.getJsgrGongjjSs());                   //个人公积金
                tmp_data.put("psickness",serviceSettlementDetailEntity.getJsgrDabSs());               //个人大病
                tmp_data.put("ptotal",serviceSettlementDetailEntity.getJsgrPersonsubtotalSs());                  //个人小计
                tmp_data.put("remark4","");                 //备用
                tmp_data.put("oweal",serviceSettlementDetailEntity.getJsqtFulifSs());                   //其它福利费
                tmp_data.put("pmaternity",serviceSettlementDetailEntity.getJsgrShengySs());              //个人生育
                tmp_data.put("remark1","");                 //备用
                tmp_data.put("cDepartment",serviceSettlementDetailEntity.getDepartment());             //员工部门
                tmp_data.put("oeInjury",serviceSettlementDetailEntity.getJsqtGongsSs());                //其它工伤
                tmp_data.put("ouInsurance","");             //失业补差
                tmp_data.put("remark3","");                 //备用
                tmp_data.put("remark2","");                 //备用
                tmp_data.put("employeeName",serviceSettlementDetailEntity.getName());            //员工姓名
                tmp_data.put("usickness",serviceSettlementDetailEntity.getJsdwDabSs());               //单位大病
                tmp_data.put("Batch",serviceSettlementDetailEntity.getBatch());                   //批次
                tmp_data.put("umaternity",serviceSettlementDetailEntity.getJsdwShengySs());              //单位生育
                tmp_data.put("opTex",serviceSettlementDetailEntity.getJsqtPersontaxSs());                   //其它个税
                tmp_data.put("omeInsurance","");            //医疗补差
                tmp_data.put("oExplain",serviceSettlementDetailEntity.getJsqtExplainSs());                //其它说明
                tmp_data.put("year",serviceSettlementDetailEntity.getYear());                    //年
                tmp_data.put("osInsurance","");             //大病补差
                tmp_data.put("punemployment",serviceSettlementDetailEntity.getJsgrShiySs());           //个人失业
                tmp_data.put("oFood",serviceSettlementDetailEntity.getJsqtFoodSs());                   //其它伙食
                tmp_data.put("osSecuallow",serviceSettlementDetailEntity.getJsqtShebaobzSs());             //其它社保补助
                tmp_data.put("ofInsurance","");             //公积金补差
                tmp_data.put("ophysicalexam",serviceSettlementDetailEntity.getJsqtTijianSs());           //其它体检费
                tmp_data.put("oprotection",serviceSettlementDetailEntity.getJsqtLaobaoSs());             //其它劳保用品
                tmp_data.put("IDnumber",serviceSettlementDetailEntity.getCardno());                //身份证号
                tmp_data.put("utota",serviceSettlementDetailEntity.getJsdwUnitsubtotalSs());                   //单位小计
                tmp_data.put("pmedical",serviceSettlementDetailEntity.getJsgrYilSs());                //个人医疗
                tmp_data.put("opInsurance","");             //养老补差
                tmp_data.put("ufund",serviceSettlementDetailEntity.getJsdwGongjjSs());                   //单位公积金
                tmp_data.put("employeesID",serviceSettlementDetailEntity.getEmpid());             //员工编号
                tmp_data.put("ounionMedicalhelp",serviceSettlementDetailEntity.getJsqtGonghyilSs());       //其它工工会医疗互助
                tmp_data.put("uemploymentinjury",serviceSettlementDetailEntity.getJsdwGongsSs());       //单位工伤
                tmp_data.put("umedical",serviceSettlementDetailEntity.getJsdwYilSs());                //单位医疗
                tmp_data.put("omaInsurance","");            //生育补差
                tmp_data.put("LineTotal",serviceSettlementDetailEntity.getTotalPaid());               //实收总额
                tmp_data.put("ototal",serviceSettlementDetailEntity.getJsqtSubtotalSs());                  //其它小计
                tmp_data.put("oeCompens",serviceSettlementDetailEntity.getJsqtJingjibcSs());               //其它经济补偿
                tmp_data.put("month",serviceSettlementDetailEntity.getMonth());                   //月
                tmp_data.put("uUnemployment",serviceSettlementDetailEntity.getJsdwShiySs());           //单位失业
                tmp_data.put("ppension",serviceSettlementDetailEntity.getJsgrYanglSs());                //个人养老
                tmp_data.put("upension",serviceSettlementDetailEntity.getJsdwYanglSs());                //单位养老
                tmp_data.put("usalary",serviceSettlementDetailEntity.getJsdwSalarySs());                 //单位工资
                tmp_data.put("uovertime",serviceSettlementDetailEntity.getJsdwOvertimepaySs());           //单位加班
                settlementDetails.add(FastJsonUtils.collectToString(tmp_data));
            }
            paramsBySettleId.put("item",settlementDetails.toString());
            paramsBySettleId.put("ServType","");            //业务类型
            paramsBySettleId.put("DocDate","");             //单据日期
            paramsBySettleId.put("CardCode","");            //客户编号
            paramsBySettleId.put("DocTotal","");            //单据总计
            paramsBySettleId.put("unBalance","");           //未清金额
            data.add(FastJsonUtils.collectToString(paramsBySettleId));
         }

        String key = Global.getConfig("b1key");// 获取key
        String url = Global.getConfig("b1url6");// 获取url
        queryParams.put("data", "");
        String sign = Sha1Util.SHA1(queryParams, key);// 计算签名
        queryParams.put("sign", sign);

        logger.info("data:" + data.toString());
        logger.info("url:" + url);
        logger.info("sign:" + sign);
        String result = HttpClientUtils.doPost(url, queryParams);// 发送请求
        if (result != null) {
            result = result.replace("null", "''");
            Map<String, Object> b1Map = FastJsonUtils.stringToCollect(result);
            if (b1Map != null && b1Map.size() > 0) {
                if (b1Map.get("isSuc") != null && (b1Map.get("isSuc").equals("y"))) {
                    //接口成功掉用后处理


                }
            }

        }


    }

    /**
     * 报销单 同步B1接口
     * @throws Exception
     */
    @Override
    @Transactional
    public void expenseSettleSyncToB1() throws Exception {
        Map<String, Object> queryParams = new HashMap<>();
        List<String> data = new ArrayList(); //请求中data参数集合

        for (Object obj: data) {

            Map<String, Object> dataUnit = new HashedMap();
            List<String> item = new ArrayList(); //明细集合
            dataUnit.put("DocNum",""); //单据编号
            for (Object obj2: data) {
                Map<String, Object> itemSingle = new HashedMap();
                itemSingle.put("Name","");                          //员工姓名
                itemSingle.put("ForecastAcct","");                  //预算科目
                itemSingle.put("AcctCode","");                      //报销科目
                itemSingle.put("PayAcount","");                     //支付科目
                itemSingle.put("Dept","");                          //部门
                itemSingle.put("LineMemo","");                      //备注
                itemSingle.put("PayAmount","");                     //支付金额
                itemSingle.put("Employee","");                      //员工编号
                item.add(FastJsonUtils.collectToString(itemSingle));
            }
            dataUnit.put("item",item);
            dataUnit.put("Memo","");                                //备注
            dataUnit.put("DocDate","");                             //单据日期
        }
        String key = Global.getConfig("b1key");// 获取key
        String url = Global.getConfig("b1url10");// 获取url
        queryParams.put("data", "");
        String sign = Sha1Util.SHA1(queryParams, key);// 计算签名
        queryParams.put("sign", sign);

        logger.info("data:" + "");
        logger.info("url:" + url);
        logger.info("sign:" + sign);
        String result = HttpClientUtils.doPost(url, queryParams);// 发送请求
        if (result != null) {
            result = result.replace("null", "''");
            Map<String, Object> b1Map = FastJsonUtils.stringToCollect(result);
            if (b1Map != null && b1Map.size() > 0) {
                if (b1Map.get("isSuc") != null && (b1Map.get("isSuc").equals("y"))) {
                    //接口成功掉用后处理


                }
            }


        }

    }

    /**
     * 更新客户发票号 同步B1接口
     * @throws Exception
     */
    @Override
    @Transactional
    public void customInvNoSyncToB1() throws Exception {
        Map<String, Object> queryParams = new HashMap<>();
        List<String> data = new ArrayList(); //请求中data参数集合

        String DocNum = "A1001"; //单据编号
        String INNum = "";  //发票号

        String key = Global.getConfig("b1key");// 获取key
        String url = Global.getConfig("b1url11");// 获取url
        queryParams.put("DocNum", DocNum);
        queryParams.put("INNum", "");
        String sign = Sha1Util.SHA1(queryParams, key);// 计算签名
        queryParams.put("sign", sign);

        logger.info("data:" + "");
        logger.info("url:" + url);
        logger.info("sign:" + sign);
        String result = HttpClientUtils.doPost(url, queryParams);// 发送请求
        if (result != null) {
            result = result.replace("null", "''");
            Map<String, Object> b1Map = FastJsonUtils.stringToCollect(result);
            if (b1Map != null && b1Map.size() > 0) {
                if (b1Map.get("isSuc") != null && (b1Map.get("isSuc").equals("y"))) {
                    //接口成功掉用后处理


                }
            }


        }

    }

    /**
     * 结算单回款情况查询 同步B1接口
     * @throws Exception
     */
    @Override
    @Transactional
    public void getPayInfoSyncToB1() throws Exception {
        Map<String, Object> queryParams = new HashMap<>();
        List<String> data = new ArrayList(); //请求中data参数集合

        String docNum = ""; //结算单号

        //拼接参数
        String key = Global.getConfig("b1key");// 获取key
        String url = Global.getConfig("b1url12");// 获取url
        queryParams.put("data", "");
        String sign = Sha1Util.SHA1(queryParams, key);// 计算签名
        queryParams.put("sign", sign);

        logger.info("data:" + "");
        logger.info("url:" + url);
        logger.info("sign:" + sign);
        String result = HttpClientUtils.doPost(url, queryParams);// 发送请求
        if (result != null) {
            result = result.replace("null", "''");
            Map<String, Object> b1Map = FastJsonUtils.stringToCollect(result);
            if (b1Map != null && b1Map.size() > 0) {
                if (b1Map.get("isSuc") != null && (b1Map.get("isSuc").equals("y"))) {
                    //接口成功掉用后处理


                }
            }

        }

    }

    /**
     * 社保局结算接口 同步B1接口
     * @throws Exception
     */
    @Override
    @Transactional
    public void addSocialSBSyncToB1() throws Exception {
        Map<String, Object> queryParams = new HashMap<>();
        List<String> data = new ArrayList(); //请求中data参数集合

        //1.查出所有的结算点
        List<CustomSettlementEntity> customSettlementEntities = customSettlementDao.getSettlementList();

        for (CustomSettlementEntity customSettlementEntity: customSettlementEntities) {

            Map<String, Object> dataUnit = new HashedMap();
            List<String> item = new ArrayList(); //明细集合

            //查询结算点下的社保缴付明细,以后可以带分年分月
            Map<String,Object> params = new HashedMap();
            params.put("settlementid",customSettlementEntity.getId());
            List<SocialSecurityPayDetailEntity> socialSecurityPayDetailEntitites = socialSecurityPayDetailDao.getPayDetailBySettlementid(params);

            for (SocialSecurityPayDetailEntity socialSecurityPayDetailEntitite: socialSecurityPayDetailEntitites) {
                Map<String, Object> itemSingle = new HashedMap();
                itemSingle.put("employeeID",socialSecurityPayDetailEntitite.getEmpid());    //员工编号
                itemSingle.put("unitpayable",socialSecurityPayDetailEntitite.getUnitPayable());   //单位应付金额
                itemSingle.put("totalcost","");     //合计费用
                itemSingle.put("setyear",socialSecurityPayDetailEntitite.getYear());       //结算点年
                itemSingle.put("actsetmonth",socialSecurityPayDetailEntitite.getMonth());   //实际结算月
                itemSingle.put("IDentityNumber",socialSecurityPayDetailEntitite.getCardNo());//身份证号码
                itemSingle.put("setmonth",socialSecurityPayDetailEntitite.getMonth());      //结算点月
                itemSingle.put("remark1","");       //备用1
                itemSingle.put("remark2","");       //备用2
                itemSingle.put("employeeName",socialSecurityPayDetailEntitite.getName());  //员工姓名
                itemSingle.put("setname",socialSecurityPayDetailEntitite.getUnitName());       //结算点名称
                itemSingle.put("insurancetype",socialSecurityPayDetailEntitite.getSocialSecurityType()); //险种
                itemSingle.put("personpayable",socialSecurityPayDetailEntitite.getPersonPayable()); //个人应付金额
                itemSingle.put("settlementID",socialSecurityPayDetailEntitite.getUnitId());  //结算点指定ID

            }
            dataUnit.put("item",item);
            dataUnit.put("DocNum","");      //唯一标识
            dataUnit.put("CardCode",customSettlementEntity.getUnitId());  //单位代码
            dataUnit.put("CardName",customSettlementEntity.getUnitName());  //单位名称
            dataUnit.put("ServType","1");  //业务类型


        }

        //拼接参数
        String key = Global.getConfig("b1key");// 获取key
        String url = Global.getConfig("b1url13");// 获取url
        queryParams.put("data", "");
        String sign = Sha1Util.SHA1(queryParams, key);// 计算签名
        queryParams.put("sign", sign);

        logger.info("data:" + "");
        logger.info("url:" + url);
        logger.info("sign:" + sign);
        String result = HttpClientUtils.doPost(url, queryParams);// 发送请求
        if (result != null) {
            result = result.replace("null", "''");
            Map<String, Object> b1Map = FastJsonUtils.stringToCollect(result);
            if (b1Map != null && b1Map.size() > 0) {
                if (b1Map.get("isSuc") != null && (b1Map.get("isSuc").equals("y"))) {
                    //接口成功掉用后处理


                }
            }

        }


    }

    /**
     * 社保局付款 同步B1接口
     * @throws Exception
     */
    @Override
    @Transactional
    public void socialsbSettleSyncToB1() throws Exception {
        Map<String, Object> queryParams = new HashMap<>();
        List<String> data = new ArrayList(); //请求中data参数集合

        String docNum = "AOO1";         //结算单号
        String payAccount = "2230250021140083";     //付款账户

        //拼接参数
        String key = Global.getConfig("b1key");// 获取key
        String url = Global.getConfig("b1url14");// 获取url
        queryParams.put("docNum", docNum);
        queryParams.put("payAccount", payAccount);
        String sign = Sha1Util.SHA1(queryParams, key);// 计算签名
        queryParams.put("sign", sign);

        logger.info("data:" + "");
        logger.info("url:" + url);
        logger.info("sign:" + sign);
        String result = HttpClientUtils.doPost(url, queryParams);// 发送请求
        if (result != null) {
            result = result.replace("null", "''");
            Map<String, Object> b1Map = FastJsonUtils.stringToCollect(result);
            if (b1Map != null && b1Map.size() > 0) {
                if (b1Map.get("isSuc") != null && (b1Map.get("isSuc").equals("y"))) {
                    //接口成功掉用后处理


                }
            }

        }

    }














}
